using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using FinanceStats.Entities;
using FinanceStats.Common;
namespace FinanceStats.DAL
{
	public static class ImportitemDAO
	{
		#region Insert
		private static DbCommand insertCommand = null;
		private static string insertSQL = "INSERT INTO ACS.dbo.ImportItem (ITEMNAME, DATE, AMOUNT) VALUES (@ITEMNAME, @DATE, @AMOUNT) SET @IMPORTITEMID = SCOPE_IDENTITY()";
		private static DbCommand GetInsertCommand(SqlDatabase db)
		{
			if (insertCommand == null)
			{
				insertCommand = db.GetSqlStringCommand(insertSQL);
				db.AddOutParameter(insertCommand, "@IMPORTITEMID", DbType.Int64, 8);
				db.AddInParameter(insertCommand, "@ITEMNAME", DbType.String);
				db.AddInParameter(insertCommand, "@DATE", DbType.DateTime);
				db.AddInParameter(insertCommand, "@AMOUNT", DbType.Decimal);
			}
			return insertCommand;
		}
		private static void SetInsertValues(SqlDatabase db, DbCommand cmd, Importitem entity)
		{
			db.SetParameterValue(cmd, "@ITEMNAME", entity.Itemname);
			db.SetParameterValue(cmd, "@DATE", entity.Date);
			db.SetParameterValue(cmd, "@AMOUNT", entity.Amount);
		}
		public static bool InsertImportitem(Importitem entity)
		{
			bool res = false;
			SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetWebConfigValue(Constants.ACSConnectionString));
			DbCommand cmd = GetInsertCommand(db);
			SetInsertValues(db, cmd, entity);
			res = Convert.ToBoolean(db.ExecuteNonQuery(cmd));
			if (res)
			{
				entity.Importitemid = (long?)db.GetParameterValue(cmd, "@IMPORTITEMID");
			}
			return res;
		}
		public static bool InsertListImportitem(List<Importitem> list)
		{
			bool res = true;
			DbTransaction transactionObj = null;
			DbConnection conn = null;
			try
			{
				SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetWebConfigValue(Constants.ACSConnectionString));
				DbCommand cmd = GetInsertCommand(db);
				conn = db.CreateConnection();
				conn.Open();
				transactionObj = conn.BeginTransaction();
				for(int i=0; i<list.Count; i++)
				{
					Importitem entity = list[i];
					SetInsertValues(db, cmd, entity);
					res = res & Convert.ToBoolean(db.ExecuteNonQuery(cmd, transactionObj));
					if (res)
					{
						entity.Importitemid = (long?)db.GetParameterValue(cmd, "@IMPORTITEMID");
					}
				}
				transactionObj.Commit();
				transactionObj.Dispose();
				conn.Close();
				conn.Dispose();
			}
			catch (Exception ex)
			{
				res = false;
				if (transactionObj != null)
				{
					transactionObj.Rollback();
					transactionObj.Dispose();
				}
				if (conn != null & conn.State != ConnectionState.Closed)
				{
					conn.Close();
					conn.Dispose();
				}
				throw ex;
			}
			return res;
		}
		#endregion
		#region Update
		private static DbCommand updateCommand = null;
		private static string updateSQL = "UPDATE ACS.dbo.ImportItem SET ITEMNAME = @ITEMNAME, DATE = @DATE, AMOUNT = @AMOUNT WHERE IMPORTITEMID = @IMPORTITEMID";
		private static DbCommand GetUpdateCommand(SqlDatabase db)
		{
			if (updateCommand == null)
			{
				updateCommand = db.GetSqlStringCommand(updateSQL);
				db.AddInParameter(updateCommand, "@IMPORTITEMID", DbType.Int64);
				db.AddInParameter(updateCommand, "@ITEMNAME", DbType.String);
				db.AddInParameter(updateCommand, "@DATE", DbType.DateTime);
				db.AddInParameter(updateCommand, "@AMOUNT", DbType.Decimal);
			}
			return updateCommand;
		}
		private static void SetUpdateValues(SqlDatabase db, DbCommand cmd, Importitem entity)
		{
			db.SetParameterValue(cmd, "@IMPORTITEMID", entity.Importitemid);
			db.SetParameterValue(cmd, "@ITEMNAME", entity.Itemname);
			db.SetParameterValue(cmd, "@DATE", entity.Date);
			db.SetParameterValue(cmd, "@AMOUNT", entity.Amount);
		}
		public static bool UpdateImportitem(Importitem entity)
		{
			bool res = false;
			SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetWebConfigValue(Constants.ACSConnectionString));
			DbCommand cmd = GetUpdateCommand(db);
			SetUpdateValues(db, cmd, entity);
			res = Convert.ToBoolean(db.ExecuteNonQuery(cmd));
			return res;
		}
		public static bool UpdateListImportitem(List<Importitem> list)
		{
			bool res = true;
			DbTransaction transactionObj = null;
			DbConnection conn = null;
			try
			{
				SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetWebConfigValue(Constants.ACSConnectionString));
				DbCommand cmd = GetUpdateCommand(db);
				conn = db.CreateConnection();
				conn.Open();
				transactionObj = conn.BeginTransaction();
				for(int i=0; i<list.Count; i++)
				{
					Importitem entity = list[i];
					SetUpdateValues(db, cmd, entity);
					res = res & Convert.ToBoolean(db.ExecuteNonQuery(cmd, transactionObj));
				}
				transactionObj.Commit();
				transactionObj.Dispose();
				conn.Close();
				conn.Dispose();
			}
			catch (Exception ex)
			{
				res = false;
				if (transactionObj != null)
				{
					transactionObj.Rollback();
					transactionObj.Dispose();
				}
				if (conn != null & conn.State != ConnectionState.Closed)
				{
					conn.Close();
					conn.Dispose();
				}
				throw ex;
			}
			return res;
		}
		
		#endregion
		#region Delete
		private static DbCommand deleteCommand = null;
		private static string deleteSQL = "DELETE FROM ACS.dbo.ImportItem WHERE IMPORTITEMID = @IMPORTITEMID";
		private static DbCommand GetDeleteCommand(SqlDatabase db)
		{
			if (deleteCommand == null)
			{
				deleteCommand = db.GetSqlStringCommand(deleteSQL);
				db.AddInParameter(deleteCommand, "@IMPORTITEMID", DbType.Int64);
			}
			return deleteCommand;
		}
		private static void SetDeleteValues(SqlDatabase db, DbCommand cmd, Importitem entity)
		{
			db.SetParameterValue(cmd, "@IMPORTITEMID", entity.Importitemid);
		}
		public static bool DeleteImportitem(Importitem entity)
		{
			bool res = false;
			SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetWebConfigValue(Constants.ACSConnectionString));
			DbCommand cmd = GetDeleteCommand(db);
			SetDeleteValues(db, cmd, entity);
			res = Convert.ToBoolean(db.ExecuteNonQuery(cmd));
			return res;
		}
		public static bool DeleteListImportitem(List<Importitem> list)
		{
			bool res = true;
			DbTransaction transactionObj = null;
			DbConnection conn = null;
			try
			{
				SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetWebConfigValue(Constants.ACSConnectionString));
				DbCommand cmd = GetDeleteCommand(db);
				conn = db.CreateConnection();
				conn.Open();
				transactionObj = conn.BeginTransaction();
				for(int i=0; i<list.Count; i++)
				{
					Importitem entity = list[i];
					SetDeleteValues(db, cmd, entity);
					res = res & Convert.ToBoolean(db.ExecuteNonQuery(cmd, transactionObj));
				}
				transactionObj.Commit();
				transactionObj.Dispose();
				conn.Close();
				conn.Dispose();
			}
			catch (Exception ex)
			{
				res = false;
				if (transactionObj != null)
				{
					transactionObj.Rollback();
					transactionObj.Dispose();
				}
				if (conn != null & conn.State != ConnectionState.Closed)
				{
					conn.Close();
					conn.Dispose();
				}
				throw ex;
			}
			return res;
		}
		
		#endregion
		#region Select
		private static string selectSQL = "SELECT IMPORTITEMID,ITEMNAME,DATE,AMOUNT FROM ACS.dbo.ImportItem {0} {1}";
		
		public static List<Importitem> LoadImportitems(string where, string orderby)
		{
			List<Importitem> res = new List<Importitem>();
			string sql = String.Format(selectSQL, where, orderby);
			IDataReader reader = null;
			try
			{
				SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetWebConfigValue(Constants.ACSConnectionString));
				reader = db.ExecuteReader(CommandType.Text, sql);
				if (reader != null)
				{
					while (reader.Read())
					{
						Importitem entity = new Importitem();
						entity.Importitemid = reader.IsDBNull(0) ? null : (long?)reader.GetInt64(0);
						entity.Itemname = reader.IsDBNull(1) ? null : (string)reader.GetString(1);
						entity.Date = reader.IsDBNull(2) ? null : (DateTime?)reader.GetDateTime(2);
						entity.Amount = reader.IsDBNull(3) ? null : (Decimal?)reader.GetDecimal(3);
						res.Add(entity);
					}
				}
				reader.Close();
			}
			catch (Exception ex)
			{
				if (reader != null && !reader.IsClosed)
				{
					reader.Close();
				}
				res = null;
				throw ex;
			}
			return res;
		}
		#endregion
	}
}
